
#pragma once

#include "MyConfig.h"

using namespace Zen;
using namespace Zen::Vap;

class FiguresLayer : public Combination2D
{
	Size2 vs;
public:
	FiguresLayer()
	{
	}

	void load() override
	{
		vs = Activity::Get()->getRealViewSize();
		loadPolygon(4);
		loadPolygon(8);
		loadPolygon(12);

		loadWheel(16, 0.8f, 1.f);
		loadWheel(16, 0.f, 1.f);
		loadWheel(8, 0.8f, 1.f);
		loadWheel(4, 0.5f, 1.f);


		loadStar(16, 0.8f, 1.f);
		loadStar(16, 0.1f, 1.f);
		loadStar(8, 0.8f, 1.f);
		loadStar(4, 0.5f, 1.f);

//		loadTexture(16, 0.8f, 1.f, {50, 50});
//		loadTexture(16, 0.f, 1.f, {50, 50});
//		loadTexture(8, 0.8f, 1.f, {50, 50});
//		loadTexture(4, 0.5f, 1.f, {50, 50});
	}
	void loadPolygon(size_t n)
	{
		auto content = FigureContentLoader::Get()->createPolygon(n, 0);
		auto tex = std::make_shared<FigureTexture>();
		this->addNode(tex);
		tex->setFigureContent(content);
		tex->setTexture(TexturesCache::Get()->getTexture("res/s.jpg"));
		tex->setPosition({gConfig.ra.nextf() * vs.w, gConfig.ra.nextf() * vs.h});
		tex->setContentSize({100, 100});
	}
	void loadWheel(size_t n, float near, float far)
	{
		auto content = FigureContentLoader::Get()->createWheel(n, 0, near, far);
		auto tex = std::make_shared<FigureTexture>();
		this->addNode(tex);
		tex->setFigureContent(content);

		tex->setTexture(TexturesCache::Get()->getTexture("res/s.jpg"));
		tex->setPosition({gConfig.ra.nextf() * vs.w, gConfig.ra.nextf() * vs.h});
		tex->setContentSize({50, 50});
		tex->runAction(ActionRotate2D::Create(tex, 1000.f, ZEN_F_PI/10.f));
//		tex->setColor(Color4f(5, 5, 5, 1.f));
		tex->setGreyfied(true);
		tex->setGreyFactor({1.f, 0.f, 0.f});
	}
	void loadStar(size_t n, float near, float far)
	{

		auto content = FigureContentLoader::Get()->createStar(n, 0, near, far);
			auto tex = std::make_shared<FigureTexture>();
			this->addNode(tex);
			tex->setFigureContent(content);

		tex->setTexture(TexturesCache::Get()->getTexture("res/s.jpg"));
		tex->setPosition({gConfig.ra.nextf() * vs.w, gConfig.ra.nextf() * vs.h});
		tex->setContentSize({100, 100});

		tex->setColor(Color4f(2, 2, 2, 1.f));

		tex->runAction(ActionRotate2D::Create(tex, 1000.f, -ZEN_F_PI/10.f));

	}
	void loadTexture(int ac, float n, float f, Size2 size)
	{
		auto vs = Activity::Get()->getRealViewSize();
		
		auto tex = std::make_shared<FigureTexture>();
		this->addNode(tex);
		auto content = std::make_shared<FigureTextureContent>();
		tex->setFigureContent(content);

		tex->setTexture(TexturesCache::Get()->getTexture("res/s.jpg"));
		tex->setPosition({gConfig.ra.nextf() * vs.w, gConfig.ra.nextf() * vs.h});


		float rad_i = ZEN_F_PI / (float)ac;
		float rad = 0;

		FigureTextureContent::VertexData e;
		for(int i = 0; i < ac*2; ++i)
		{
			float x = cosf(rad) * ((i%2)?n:f);
			float y = sinf(rad) * ((i%2)?n:f);

			e.coord = { x, y };
			e.tex_coord = { (x+1)/2.f, 1-((y+1)/2.f) };
			content->addVertex(e);
			rad += rad_i;
//			e.setscal
		}
		content->setStrip(1);
		content->addVertex(content->getVertex(0));
		content->addVertex(content->getVertex(1));

		tex->runAction(ActionRotate2D::Create(tex, 1000.f, ZEN_F_PI/10.f));
	}
};
